//
//  WebViewController.m
//  xgt
//
//  Created by  程 强 on 15/7/1.
//  Copyright (c) 2015年 Demon. All rights reserved.
//

#import "WebViewController.h"
#import "JTCSM.h"

@interface WebViewController ()<UIWebViewDelegate>
{
    BOOL isF;
}
@end

@implementation WebViewController

//返回父控制器
- (void)gotoBack{
    if([self.isruzhu isEqualToString:@"wangzheguanjia"]){
       [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
       [self.navigationController popViewControllerAnimated:YES];
    }
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [df isRemoveTarbar:YES];
    self.navigationController.navigationBar.hidden = NO;
    [JTCSMSingle shareSingle].navigationC = self.navigationController;
    UILabel *label = [UILabel initWithFrame:CGRectMake(0, 0, 60, 40) text:self.titleName font:18 textAlignment:NSTextAlignmentCenter texcolor:[UIColor blackColor] backgroundColor:[UIColor clearColor]];
//    label.numberOfLines = 2;
    [self.navigationItem setTitleView:label];
    //设置barbuttonitem
    leftBarButtonItems_rightBarButtonItems *button = KCUSTOMNAVIGSTION_ALLOC;
    NSArray *items = @[[button setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotoBack)]];
    self.navigationItem.leftBarButtonItems = items;
    

}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar.png"] forBarMetrics:UIBarMetricsDefault];
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat space = 0;
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(space, space, KCONTROLLERWith-2*space, KCONTROLLERHeight-space)];
    webView.delegate = self;
    webView.scrollView.bounces = NO;
    [df showWaiting:webView];
    
    
    if ([self.htmlName isEqualToString:@""] || self.htmlName == nil) {
        NSURL *url = [NSURL URLWithString:self.urlStr];
        NSLog(@"%@",url);
        
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
        [webView loadRequest:request];
        
        
    }else{
   
        NSString*htmlstring=[[NSString alloc]initWithContentsOfFile:self.htmlName encoding:NSUTF8StringEncoding error:nil];
        [webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[ [NSBundle mainBundle] bundlePath]]];
    }
    
    [self.view addSubview:webView];
    // Do any additional setup after loading the view.
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    if ( navigationType == UIWebViewNavigationTypeBackForward ) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }
    NSLog(@"%@",request);
    NSString * urlstr = [NSString stringWithFormat:@"%@",request.URL];
    if ([urlstr containsString:@"https://itunes.apple.com/cn/app"]) {
        [self gotoBack];
    }
    return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [df hideWaiting:webView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
